import { createApp } from 'vue';
import '@/global.scss';
import App from './App.vue';
import { setupStore } from '@/store';
import { setupRouter } from '@/router';
import { setupDirective } from './directive';
import { emitter, EmitterEvent, setupUtils } from '@great/utils';
import { ElNotification } from 'element-plus';
import { REQUEST_CODE_MESSAGE } from '@/utils/constant.ts';
import { setupPlugin } from './utils/plugin.ts';
import { setupComponents } from '@great/components';

// 注册图标
const app = createApp(App);

setupRouter(app);
setupStore(app);
setupDirective(app);
setupPlugin(app);
setupUtils(app);
setupComponents(app, ['//at.alicdn.com/t/c/font_5014598_6kxdox5ag0w.js']);

app.mount('#app');

// 订阅全局Request异常
emitter.on(EmitterEvent.REQUEST_FAILED, ({ title, code, message }) => {
    if (code === 401) {
        ElNotification({
            message: message || REQUEST_CODE_MESSAGE[code],
            type: 'error'
        });
        if (localStorage.token) {
            localStorage.clear();
            sessionStorage.clear();
            setTimeout(() => {
                window.location.href = '/login';
            }, 1500);
        }
        return;
    }
    ElNotification({
        title: code === 403 ? undefined : title,
        message: message || REQUEST_CODE_MESSAGE[code],
        type: 'error'
    });
});
